function showAddDialog() {
	
	try {
		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
	    
	    var main = Components.classes['@downloadbinning.net/main;1'].getService(Components.interfaces.nsIDownloadBinning);
	    
		var params = {inn:null, out:null};
		window.openDialog("db-addFilter.xul", "Download Binning - Basic Mode", "modal, centerscreen", params );
		if( params.out ) {
			var new_rule;
		    new_rule = Components.classes["@downloadbinning.net/rule;1"].createInstance(Components.interfaces.nsIDownloadBinningRule);
		    new_rule.setName(params.out.name);
		    new_rule.setDescription(params.out.description);
		    new_rule.setDomain(params.out.dnPattern, params.out.dnIsRegex );
		    new_rule.setFilename(params.out.fnPattern, params.out.fnIsRegex);
		    new_rule.setDestination(params.out.path);
		    new_rule.setEnabled(true);
		    var idx = main.addRule(new_rule);
		    if (idx < 0) {
		    	alert("Error while adding a new rule");
		    }
		    
		    var listBox = document.getElementById( "filters" );
		    while( listBox.getRowCount() > 0 ) {
		    	listBox.removeItemAt(0);
		    }
		    init();
		}
	} catch (anError) {
		dump("ERROR: " + anError);
	    alert("Error: " + anError);
	}
}

function showEditDialog() {
	
	var listBox = document.getElementById( "filters" );
	if( listBox.selectedCount == 1 ) {
		try {
			netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
		    
		    var main = Components.classes['@downloadbinning.net/main;1'].getService(Components.interfaces.nsIDownloadBinning);
		    
		    var index = listBox.selectedIndex;
		    var rule = main.getRule( index );
		    
			var params = {inn:{ruleToEdit:rule}, out:null};
			window.openDialog("db-addFilter.xul", "Download Binning - Basic Mode", "modal, centerscreen", params );
			if( params.out ) {
				var new_rule;
			    new_rule = Components.classes["@downloadbinning.net/rule;1"].createInstance(Components.interfaces.nsIDownloadBinningRule);
			    new_rule.setName(params.out.name);
			    new_rule.setDescription(params.out.description);
			    new_rule.setDomain(params.out.dnPattern, params.out.dnIsRegex );
			    new_rule.setFilename(params.out.fnPattern, params.out.fnIsRegex);
			    new_rule.setDestination(params.out.path);
			    new_rule.setEnabled(true);
			    var idx = main.updateRule( index, new_rule);
			    if (idx < 0) {
			    	alert("Error while updating the rule.");
			    }
			    
			    
			    while( listBox.getRowCount() > 0 ) {
			    	listBox.removeItemAt(0);
			    }
			    init();
			}
		} catch (anError) {
			dump("ERROR: " + anError);
		    alert("Error: " + anError);
		}
	} else {
		alert( "Please select one rule to edit." );
	}
}
	
function openDB() {
	window.open("chrome://downloadbinning/content/db-maindialog.xul", "Download Binning - Filter List", "chrome, centerscreen");
}

function removeFilter() {
	try {
		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
	    var main = Components.classes['@downloadbinning.net/main;1'].getService(Components.interfaces.nsIDownloadBinning);
	    
	    var listBox = document.getElementById( "filters" );
	    
	    for(var i = 0; i < listBox.getRowCount(); i++) {
	    	if( listBox.getItemAtIndex(i).selected ) {
	    		listBox.removeItemAt(i);
	    		main.removeRule(i);
	    		i--;
	    	}
	    }
	} catch (anError) {
		dump("ERROR: " + anError);
	    alert("Error: " + anError);
	}
}

function moveUp() {
	var listBox = document.getElementById( "filters" );
	if( !listBox.getItemAtIndex(0).selected ) {
		try {
			netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
		    var main = Components.classes['@downloadbinning.net/main;1'].getService(Components.interfaces.nsIDownloadBinning);
		    
		    var selectedRows = new Array();
		    for(var i = 1; i < listBox.getRowCount(); i++) {
		    	if( listBox.getItemAtIndex(i).selected == true ) {
		    		main.moveUp(i);
		    		selectedRows[selectedRows.length] = i-1;
		    	}
		    }
		    while( listBox.getRowCount() > 0 ) {
		    	listBox.removeItemAt(0);
		    }
		    init();
		    if( selectedRows.length > 1 ) {
		    	listBox.selectItemRange( listBox.getItemAtIndex( selectedRows[0] ), 
		    			listBox.getItemAtIndex( selectedRows[selectedRows.length-1] ) );
		    } else {
		    	listBox.selectItem( listBox.getItemAtIndex( selectedRows[0] ) );
		    }
		} catch (anError) {
			dump("ERROR: " + anError);
		    alert("Error: " + anError);
		}
	}
}

function moveDown() {
	var listBox = document.getElementById( "filters" );
    
    if( !listBox.getItemAtIndex(listBox.getRowCount()-1).selected ) {
		try {
			netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
		    var main = Components.classes['@downloadbinning.net/main;1'].getService(Components.interfaces.nsIDownloadBinning);
		    
		    var selectedRows = new Array();
		    for( var i = listBox.getRowCount()-2; i > -1;i-- ) {
		    	if( listBox.getItemAtIndex(i).selected == true ) {
		    		main.moveDown(i);
		    		selectedRows[selectedRows.length] = i+1;
		    	}
		    }
		    
		    while( listBox.getRowCount() > 0 ) {
		    	listBox.removeItemAt(0);
		    }
		    init();
		    if( selectedRows.length > 1 ) {
		    	listBox.selectItemRange( listBox.getItemAtIndex( 
		    			selectedRows[selectedRows.length-1]), 
		    			listBox.getItemAtIndex( selectedRows[0] ) );
		    } else {
		    	listBox.selectItem( listBox.getItemAtIndex(selectedRows[0]) );
		    }
		} catch (anError) {
			dump("ERROR: " + anError);
		    alert("Error: " + anError);
		}
	}
	
	
}

function init() {
	try {
		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
	    
	    var main = Components.classes['@downloadbinning.net/main;1'].getService(Components.interfaces.nsIDownloadBinning);
	    var rules = main.getRules({});
	    
	    for (var i = 0; i < rules.length; i++) {
	    	var r = rules[i];
	    	var newFilter = document.createElement("listitem");
	    	newFilter.setAttribute( "allowevents", true );
			cell = document.createElement("listcell");
			cell.setAttribute("label", r.getName());
			cell.setAttribute("style", "text-align:left");
			newFilter.appendChild( cell );
			cell = document.createElement("listcell");
			cell.setAttribute("label", r.getDescription());
			cell.setAttribute("style", "text-align:left");
			newFilter.appendChild( cell );
			cell = document.createElement("listcell");
			cell.setAttribute("label", r.getDomain());
			cell.setAttribute("style", "text-align:left");
			newFilter.appendChild( cell );
			cell = document.createElement("listcell");
			cell.setAttribute("label", r.getFilename());
			cell.setAttribute("style", "text-align:left");
			newFilter.appendChild( cell );
			cell = document.createElement("listcell");
			cell.setAttribute("type", "checkbox");
			cell.setAttribute("checked", r.getEnabled() );
			cell.setAttribute("onclick", "toggleEnabled()" );
			cell.setAttribute("style", "center" );
			newFilter.appendChild( cell );
			cell = document.createElement("listcell");
			cell.setAttribute("label", r.getDestination() );
			cell.setAttribute("style", "text-align:left");
			newFilter.appendChild( cell );
			
			document.getElementById( "filters" ).appendChild( newFilter );
	    }
	    
	}catch (anError) {
		dump("ERROR: " + anError);
	    alert("Error: " + anError);
	}
}

function toggleEnabled() {
	try {
		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
	    var main = Components.classes['@downloadbinning.net/main;1'].getService(Components.interfaces.nsIDownloadBinning);
	    
	    var listBox = document.getElementById( "filters" );
	    var rule = main.getRule( listBox.selectedIndex );
	    if( rule.getEnabled() ) {
	    	rule.setEnabled( false );
	    } else {
	    	rule.setEnabled( true );
	    }
	    main.updateRule( listBox.selectedIndex, rule );
	    while( listBox.getRowCount() > 0 ) {
	    	listBox.removeItemAt(0);
	    }
	    init();
	} catch (anError) {
		dump("ERROR: " + anError);
	    alert("Error: " + anError);
	}
}